package com.szh.mysobproject.ui.fragment.user.media

import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import by.kirich1409.viewbindingdelegate.viewBinding
import com.szh.mysobproject.R
import com.szh.mysobproject.action.StatusAction
import com.szh.mysobproject.app.AppActivity
import com.szh.mysobproject.app.AppFragment
import com.szh.mysobproject.databinding.UserFollowOrFansListFragmentBinding
import com.szh.mysobproject.other.FollowState
import com.szh.mysobproject.other.IntentKey
import com.szh.mysobproject.ui.activity.ViewUserActivity
import com.szh.mysobproject.ui.adapter.AdapterDelegate
import com.szh.mysobproject.ui.adapter.UserFollowListAdapter
import com.szh.mysobproject.util.SimpleLinearSpaceItemDecoration
import com.szh.mysobproject.util.dp
import com.szh.mysobproject.util.fromJson
import com.szh.mysobproject.util.loadStateListener
import com.szh.mysobproject.viewmodel.FollowOrFansViewModel
import com.szh.mysobproject.widget.StatusLayout
import kotlinx.coroutines.flow.collectLatest

/**
 * @author : create by  szh
 * @date : 2022/7/8 17:26
 * @desc :关注fragment
 */

class UserFollowOrFansListFragment : AppFragment<AppActivity>(), StatusAction {

    private val mBinding by viewBinding<UserFollowOrFansListFragmentBinding>()
    private val mFollowViewModel by viewModels<FollowOrFansViewModel>()
    private val mAdapterDelegate = AdapterDelegate()
    private val mUserFollowListAdapter = UserFollowListAdapter(mAdapterDelegate)
    private val loadStateListener = loadStateListener(mUserFollowListAdapter) {
        mBinding.refreshLayout.finishRefresh()
    }

    override fun getLayoutId(): Int = R.layout.user_follow_or_fans_list_fragment

    override fun initView() {
        mBinding.rvFollowList.apply {
            adapter = mUserFollowListAdapter
            layoutManager = LinearLayoutManager(context)
            addItemDecoration(SimpleLinearSpaceItemDecoration(4.dp))
        }
    }

    override fun initData() {
        val userIs = arguments?.getString(IntentKey.ID) ?: ""
        val followStateJson = arguments?.getString(IntentKey.OTHER) ?: ""
        val followState = fromJson<FollowState>(followStateJson) ?: FollowState.FOLLOW
        loadUserFollowList(userIs, followState)
    }

    private fun loadUserFollowList(userIs: String, followState: FollowState) {
        viewLifecycleOwner.lifecycleScope.launchWhenCreated {
            mFollowViewModel.loadUserFollowOrFansListByState(userIs, followState).collectLatest {
                mUserFollowListAdapter.submitData(it)
            }
        }
    }

    override fun initEvent() {
        mBinding.refreshLayout.setOnRefreshListener {
            mUserFollowListAdapter.refresh()
        }
        mUserFollowListAdapter.addLoadStateListener(loadStateListener)
        mUserFollowListAdapter.setOnItemClickListener { item, _ ->
            //跳转到用户详页面
            ViewUserActivity.start(requireContext(), item.userId)
        }
    }

    override fun getStatusLayout(): StatusLayout = mBinding.hlFollowHint

    override fun onDestroyView() {
        super.onDestroyView()
        mUserFollowListAdapter.removeLoadStateListener(loadStateListener)
    }

    companion object {
        @JvmStatic
        fun newInstance(userId: String, followState: FollowState): UserFollowOrFansListFragment {
            val fragment = UserFollowOrFansListFragment()
            val args = Bundle().apply {
                putString(IntentKey.ID, userId)
                putString(IntentKey.OTHER, followState.toString())
            }
            fragment.arguments = args
            return fragment
        }
    }
}